package com.xxxx.yeb.controller;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xxxx.yeb.mapper.EmployeeMapper;
import com.xxxx.yeb.mapper.SalaryMapper;
import com.xxxx.yeb.pojo.RespBean;
import com.xxxx.yeb.pojo.Salary;

import com.xxxx.yeb.service.ISalaryService;
import com.xxxx.yeb.util.AssertUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-07-17
 */
@RestController
@RequestMapping("/salary")
public class SalaryController {

    @Resource
    private SalaryMapper salaryMapper;

    @Resource
    private EmployeeMapper employeeMapper;



    /**
     * 初始化工资帐套列表
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/sob")
    public List<Salary> getSalarys()  {
        List<Salary> salaries = salaryMapper.selectList(new QueryWrapper<Salary>());
        return salaries;
    }


    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/sob/{id}")
    public RespBean deleteById(@PathVariable Integer id) {
        //返回给前台的数据
        Boolean resp = true;
        int line = 0;
        line = salaryMapper.deleteById(id);

        if (line == 0) {
            return RespBean.error("删除失败");
        }
        return RespBean.success("删除成功");
    }

    /**
     * 添加数据
     *
     * @param req
     * @param res
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(method = RequestMethod.POST, value = "/sob")
    public Boolean addSalary(HttpServletRequest req, HttpServletResponse res) throws JsonProcessingException, Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        // 返回给前台的数据
        Boolean resp = true;
        String s = getStringFromStream(req);
        Salary salary = objectMapper.readValue(s, Salary.class);
        String name = salary.getName();
        AssertUtil.isTrue(name == null, "添加帐套的名字不能为空");
        //工资帐套名不能重复
        int count = salaryMapper.countByName(name);
        AssertUtil.isTrue(count > 0, "工资帐套名不能重复");
        //工资帐套添加时间
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        salary.setCreateDate(sdf.parse(dateNowStr));
        int line = salaryMapper.insert(salary);
        if (line == 0) {
            resp = false;
            return resp;
        }
        return resp;
    }
    /**
     * 编辑数据
     * */
    @RequestMapping(method = RequestMethod.PUT, value = "/sob")
    public Boolean editSalary(HttpServletRequest req, HttpServletResponse res) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        // 返回给前台的数据
        Boolean resp = true;
        String s = getStringFromStream(req);
        Salary salary = objectMapper.readValue(s, Salary.class);
        int line = salaryMapper.updateById(salary);
        if (line == 0) {
            resp = false;
            return resp;
        }
        return resp;
    }
    /**
     * 从Request Payload中获取数据
     *
     * */
    private String getStringFromStream(HttpServletRequest req) {
        ServletInputStream is;
        try {
            is = req.getInputStream();
            int nRead = 1;
            int nTotalRead = 0;
            byte[] bytes = new byte[10240];
            while (nRead > 0) {
                nRead = is.read(bytes, nTotalRead, bytes.length - nTotalRead);
                if (nRead > 0) {
                    nTotalRead = nTotalRead + nRead;
                }
            }
            String str = new String(bytes, 0, nTotalRead, "utf-8");
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }


}
